import { Modal } from 'ant-design-vue'
import useOutDoctorStore from '@/views/out-doctor/store'
import { post } from '@/utils/request'
/**
 * 递归获取所有子节点children获取id
 * @param {Array} data  数据源[]
 * @param {String} idKey  id的key {string}
 * @returns {Array} ids[]
 */
export const getIds = (data, idKey = 'id') => {
  const ids = []
  data.forEach((item) => {
    ids.push(item[idKey])
    if (item.children) {
      ids.push(...getIds(item.children))
    }
  })
  return ids
}
/**
 *
 * @param {*} drugStoreId  仓库id
 * @param {*} isProducer   是否是发药药房
 * @param {*} storetype    仓库类型
 * @param {*} isObj
 * @returns
 */
export const judgeDrugStoreStatus = async (drugStoreId, isProducer = '1', storetype = '1', isObj = '') => {
  const { data, code } = await post('/operation/opex/getDrugStoreInfoList', {
    isProducer,
    storetype,
    status: 1, //启用状态
    pageNum: 1,
    pageSize: 999
  })
  if (code === 0) {
    if (drugStoreId) {
      const isHave = data.find((item) => item.id === drugStoreId)
      if (isHave?.id) {
        return isObj ? isHave : drugStoreId
      } else {
        return isObj ? data[0] : data[0].id
      }
    } else {
      if (data && data.length > 0) {
        return isObj ? data[0] : data[0].id
      } else {
        return isObj ? {} : ''
      }
    }
  }
}
/**
 *
 * @param {*} arrData  目标数组
 * @param {*} value    目标值
 * @param {*} valueKey  key
 * @returns
 */
export const isHave = (arrData, value, valueKey = 'value') => {
  return arrData.some((item) => {
    return item[valueKey] === value
  })
}
/**
 * 检查是否接诊
 * @param {String} jzbh  就诊编号 {string}
 * @returns {boolean} true/false
 */
export const checkPatientStatus = async (jzbh) => {
  const res = await post('/outp/mzys/determinePatientWhetherDiagnosis ', { jzbh })
  if (res.code === 1) {
    const patientListStore = useOutDoctorStore()
    Modal.warning({
      title: '提示',
      content: `该患者已被【${res.data || '-'}】医生接诊`,
      okText: '确定',
      centered: true,
      maskClosable: false,
      width: 500,
      onOk: () => {
        patientListStore.isRefreshPatientList = true
        return false
      }
    })
  } else {
    return true
  }
}
/**
 * 判断是否是药品
 * @param {String} code  药品类型编码{string} [2,4,5]为药品
 * @description code[2,4,5]为药品
 * @returns {boolean} true/false
 */
export const isDrug = (code) => {
  if ([2, 4, 5].includes(code)) {
    return true
  } else {
    return false
  }
}
/**
 * 设置打印尺寸和方向 默认 A4 纵向portrait
 * @param {String} size  尺寸 A4 A5
 * @param {String} direction  方向 横向:landscape  纵向:portrait
 *
 */
export const setPrintSize = (size, direction) => {
  const style = document.createElement('style')
  document.head.appendChild(style)
  style.innerHTML = `
    @media print {
      @page {
        size: ${size} ${direction};
      }
    }
  `
}
